package com.abc.cloud.service.impl;

import com.abc.cloud.common.enums.FileTypeEnums;
import com.abc.cloud.common.utils.SpaceUtil;
import com.abc.cloud.dto.count.DashBoardCount;
import com.abc.cloud.dto.count.UserFileCount;
import com.abc.cloud.dto.sys.SystemSpace;
import com.abc.cloud.entity.FileEntity;
import com.abc.cloud.entity.UserEntity;
import com.abc.cloud.service.FileService;
import com.abc.cloud.service.UserService;
import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import org.springframework.beans.factory.annotation.Autowired;
import com.abc.cloud.service.CountService;
import org.springframework.stereotype.Service;

import java.util.HashMap;
import java.util.List;
import java.util.Map;


@Service
public class CountServiceImpl implements CountService {

    @Autowired
    private FileService fileService;

    @Autowired
    private UserService userService;

    @Autowired
    private SpaceUtil spaceUtil;

    @Override
    public DashBoardCount getDashboardCount() {
        DashBoardCount dashBoardCount = new DashBoardCount();

        SystemSpace systemSpace = spaceUtil.getSystemSpace();

        dashBoardCount.setTotalFile(fileService.count(new QueryWrapper<FileEntity>().eq("status", 1)));
        dashBoardCount.setTotalUser(userService.count(new QueryWrapper<UserEntity>().eq("status", 1)));
        dashBoardCount.setTotalSpace(systemSpace.getTotalSpace());
        dashBoardCount.setUseSpace(systemSpace.getUseSpace());

        return dashBoardCount;
    }

    @Override
    public List<UserFileCount> getUserFileCount(Long userId) {
        List<UserFileCount> userFileCountList = fileService.countUserFile(userId);

        Map<Integer, String> map = new HashMap<>();
        for (FileTypeEnums value : FileTypeEnums.values()) {
            map.put(value.getType(), value.getDesc());
        }
        userFileCountList.stream().forEach(item -> {
            item.setTypeName(map.get(item.getFileType()));
        });

        return userFileCountList;
    }
}
